package com.itheima.Controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cglib.core.MethodWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * reggie-com.itheima.Controller
 * Administrator
 * 2023-03-11
 * 17:13
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R saveMeal(@RequestBody SetmealDto setmealDto){
        boolean save = setmealService.save(setmealDto);
        int count=0;
        if (save){
            Long id = setmealDto.getId();
            List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
            for (SetmealDish setmealDish : setmealDishes){
                setmealDish.setSetmealId(id);
                setmealDishService.save(setmealDish);
                count++;
            }
            if (count==setmealDishes.size()){
                return  R.success("保存套餐成功");
            }
            }
        return R.error("添加失败");
        }

        @GetMapping("/page")
        public R page(Integer page,Integer pageSize,String name){
            Page<Setmeal> setmealPage = new Page<>(page,pageSize);
            Page<SetmealDto> setmealDtoPage = new Page<>();
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.like(!StringUtils.isEmpty(name),Setmeal::getName,name);
            Page<Setmeal> page1 = setmealService.page(setmealPage,lqw);
            long total = page1.getTotal();
            List<Setmeal> records = page1.getRecords();
            if (page1!=null){
                ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
                for (Setmeal record : records) {
                    Long categoryId = record.getCategoryId();
                    LambdaQueryWrapper<Category> lqw1 = new LambdaQueryWrapper<>();
                    lqw1.eq(Category::getId,categoryId);
                    Category one = categoryService.getOne(lqw1);
                    if (one!=null){
                        String name1 = one.getName();
                        SetmealDto setmealDto = new SetmealDto();
                        BeanUtils.copyProperties(record,setmealDto);
                        setmealDto.setCategoryName(name1);
                        setmealDtos.add(setmealDto);
                    }
                }
                setmealDtoPage.setRecords(setmealDtos);
                setmealDtoPage.setTotal(total);
                return R.success(setmealDtoPage);
            }
            return R.error("查询失败");
        }


        @Transactional
        @DeleteMapping
        public R deleteMeal(@RequestParam("ids") List<Long> ids){
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.in(Setmeal::getId,ids);
            lqw.eq(Setmeal::getStatus,1);
            int count = setmealService.count(lqw);
            System.out.println("的发货的方法还是"+count);
            if (count>0){
                return R.error("套餐正在使用无法删除");
            }
                setmealService.removeByIds(ids);
                LambdaQueryWrapper<SetmealDish> lqw1 = new LambdaQueryWrapper<>();
                lqw1.in(SetmealDish::getSetmealId,ids);
                setmealDishService.remove(lqw1);
                return  R.success("套餐删除成功");

        }

        @PostMapping("/status/{status}")
        public R updateStatus (@PathVariable Integer status,@RequestParam List<Long> ids){
            ArrayList<Setmeal> setmealList = new ArrayList<>();
            for (Long id : ids) {
                Setmeal setmeal = setmealService.getById(id);
                setmeal.setStatus(status);
                setmealList.add(setmeal);
            }
            boolean b = setmealService.updateBatchById(setmealList);
            if (b){
                return  R.success("操作成功");
            }
            return  R.error("操作失败");

        }

        @GetMapping("/{id}")
        public R selectById(@PathVariable Long id){
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getId,id);
            Setmeal setmeal = setmealService.getOne(lqw);
            if (setmeal!=null){
                Long categoryId = setmeal.getCategoryId();
                SetmealDto setmealDto = new SetmealDto();
                BeanUtils.copyProperties(setmeal,setmealDto);
                LambdaQueryWrapper<Category> lqw1 = new LambdaQueryWrapper<>();
                lqw1.eq(Category::getId,categoryId);
                Category category = categoryService.getOne(lqw1);
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
                LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
                lqw2.eq(SetmealDish::getSetmealId,id);
                List<SetmealDish> list = setmealDishService.list(lqw2);
                setmealDto.setSetmealDishes(list);
                System.out.println("现场VS打法干撒干撒发"+setmealDto);
                return R.success(setmealDto);
            }
            return  R.error("查询失败");
        }

        @PutMapping
        public  R updateSetmeal(@RequestBody SetmealDto setmealDto){
            boolean b = setmealService.updateById(setmealDto);
            Long setmealId = setmealDto.getId();
            ArrayList<SetmealDto> setmealDtoslist = new ArrayList<>();
            if (b){
                //清理原来菜品
                LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
                lqw.eq(SetmealDish::getSetmealId,setmealId);
                setmealDishService.remove(lqw);


                //添加新菜品
                List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
                for (SetmealDish setmealDish : setmealDishes) {
                    setmealDish.setSetmealId(setmealId);
                }
                setmealDishService.saveBatch(setmealDishes);
                return  R.success("修改成功");
            }
            return  R.error("修改失败");
        }

        //根据套餐分类id查询该套餐分类下的套餐
    @GetMapping("/list")
    @Cacheable(value = "setmeal_cache",key = "'setmeal_'+#categoryId+#status",unless = "#result==null")
    public R selectDish(Long categoryId,Integer status ){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,categoryId);
        lqw.eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(lqw);
        if (list.size()!=0){
            return R.success(list);
        }
        return  R.error("查询失败");


    }


    }
